package cn.edu.sjtu.process_management.config;

import org.dozer.DozerBeanMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;


import java.nio.charset.StandardCharsets;

/**
 * @author loumoon
 * @date 2019-11-01 15:16
 */
@Configuration
public class BeanConfig {
    @Bean
    /*注入DozerBeanMapper的Bean到Spring容器*/
    public DozerBeanMapper mapper() {
        DozerBeanMapper mapper = new DozerBeanMapper();
        return mapper;
    }

    @Bean
    /*注入RestTemplate的Bean到Spring容器*/
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();

        // 去除默认的String转换器
        restTemplate.getMessageConverters().removeIf(converter -> converter instanceof StringHttpMessageConverter);
        // 添加自定义的String转换器, 支持UTF-8
        restTemplate.getMessageConverters()
                .add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate;
    }
}